package com.zishuimuyu.job.download;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.extra.ftp.FtpConfig;
import cn.hutool.setting.Setting;
import cn.hutool.setting.SettingUtil;
import com.zishuimuyu.mqtt.MyMqttClient;
import com.zishuimuyu.mqtt.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.io.BufferedReader;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

/**
 * 定时定时任务任务-只用来上传文件
 *
 * @author：GJH
 * @date
 * @company：北京木联能软件股份有限公司
 */
@Slf4j
public class MqttDownloadJob {
    private static Setting uploadFileSetting = SettingUtil.getFirstFound("config/download_mqtt.setting");
    private static Map<String, String> fileTypes = uploadFileSetting.getMap("fileTypes");
    private static String localPath = "";
    private static String localWeatherPath = "";
    private static String localPowerPath = "";
    private static String remotePath = "";
    private static String remoteWeatherPath = "";
    private static String remotePowerPath = "";
    private static String countFilePath = "";
    private static String filePrefix = "";
    private static String fileExtension = "";
    private static String delete = "";
    @Autowired
    private MyMqttClient myMqttClient;

    /**
     * 发送主题前缀
     */
    @Value("${spring.mqtt.push-topic}")
    private String pushTopic;
    //静态代码块会在依赖注入后自动执行,并优先执行 下载
    static {
        localPath = uploadFileSetting.getStr("localPath");
        localWeatherPath = uploadFileSetting.getStr("localWeatherPath");
        localPowerPath = uploadFileSetting.getStr("localPowerPath");
        remotePath = uploadFileSetting.getStr("remotePath");
        remoteWeatherPath = uploadFileSetting.getStr("remoteWeatherPath");
        remotePowerPath = uploadFileSetting.getStr("remotePowerPath");
        countFilePath = uploadFileSetting.getStr("countFilePath");
        filePrefix = uploadFileSetting.getStr("filePrefix");
        fileExtension = uploadFileSetting.getStr("fileExtension");
        delete = uploadFileSetting.getStr("delete");
    }

    /**
     * 将要执行的方法所在的类交个spring容器扫描(@Component),并且在要执行的方法上添加@PostConstruct注解或者静态代码块执行**
     *
     * @Postcontruct’在依赖注入完成后自动调用
     */

    public void download() {
        log.info("MQTT下载数据启用：{}-> {}", localPath, remotePath);
        String protocol = uploadFileSetting.getStr("protocol");
        String host = uploadFileSetting.getStr("host");
        String charset = uploadFileSetting.getStr("Charset");
        int defaultPort = "sftp".equalsIgnoreCase(protocol) ? 22 : 21;
        Integer port = uploadFileSetting.getInt("port", defaultPort);
        String userName = uploadFileSetting.getStr("userName");
        String passWord = uploadFileSetting.getStr("passWord");
        Long sessionConnectTimeout = uploadFileSetting.getLong("sessionConnectTimeout", 2000L);
        Long channelConnectedTimeout = uploadFileSetting.getLong("channelConnectedTimeout", 2000L);
        FtpConfig ftpConfig = new FtpConfig(host, port, userName, passWord, CharsetUtil.charset(charset));
        ftpConfig.setConnectionTimeout(sessionConnectTimeout);
        ftpConfig.setSoTimeout(channelConnectedTimeout);
        FileUtil.mkdir(localPath);
        FileUtil.mkdir(localWeatherPath);
        FileUtil.mkdir(localPowerPath);
        FileUtil.mkdir(countFilePath);
        List<File> weatherFiles = FileUtil.loopFiles(localWeatherPath);
        List<File> powerFiles = FileUtil.loopFiles(localPowerPath);

        //1.获取创建文件位置
        String path = "E:/back/WPFSData";
        //2.获取备份文件存储的目录
        String dataBackupDir = "/back/WPFSDataBak/";
        File file = FileUtil.file("E:/back/WPFSData", "HA0063_tsi_202411201030.json");
        BufferedReader reader = FileUtil.getReader(file, StandardCharsets.UTF_8);

        try {
            DateTime now = DateUtil.date();
            MyMqttClient myMqttClient = SpringContextHolder.getBean(MyMqttClient.class);
            //实例文件的内容
            //获取示例文件里的图片的base64编码
            //最终要发送的文件的内容（是json文件）

            //myMqttClient.subscribe("station/weather", 1);
            myMqttClient.subscribe("station/weather");
            //myMqttClient.cleanTopic("station/weather");
            //发送成功后将发送的内容保存本地备份
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }





}
